/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.guis.gantt.items;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.GanttConstants;
import org.tbjd.client.constants.ProjectConstants;
import org.tbjd.client.constants.TimeConstants;
import org.tbjd.client.constants.ZoomConstants;
import org.tbjd.client.guis.AbstractItem;

import com.google.gwt.user.client.ui.FocusPanel;

/**
 * <p>
 * Upper class for {@link org.tbjd.client.guis.gantt.items.GanttTaskItem GanttTaskItem},
 * {@link org.tbjd.client.guis.gantt.items.GanttPhaseItem GanttPhaseItem} and GanttMilestonItem
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public abstract class AbstractGanttItem extends AbstractItem {

	/**
	 * Optional label of the AbstractItem
	 */
	private String label;
	/**
	 * Position in pixels on the 'x' axis of the AbstractItem based on the Task BeginDate
	 */
	private int xCoordinate;
	/**
	 * Position in pixels on the 'y' axis of the AbstractItem based on the Task level
	 */
	private int yCoordinate;
	/**
	 * Optional handle to drag the AbstractItem
	 */
	private FocusPanel handle;

	private List<AbstractGanttItem> lstChildItem = null;

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public int getXCoordinate() {
		return xCoordinate;
	}

	public void setXCoordinate(int coordinate) {
		xCoordinate = coordinate;
	}

	public int getYCoordinate() {
		return yCoordinate;
	}

	public void setYCoordinate(int level) {
		yCoordinate = level * (GanttConstants.DRAGGABLEGANTTITEM_HEIGHT + GanttConstants.DRAGGABLEGANTTITEM_SPACE);
	}

	public FocusPanel getHandle() {
		return handle;
	}

	public void setHandle(FocusPanel handle) {
		this.handle = handle;
	}

	public List<AbstractGanttItem> getLstChildItem() {
		return lstChildItem;
	}

	public void setLstChildItem(List<AbstractGanttItem> childItems) {
		this.lstChildItem = childItems;
	}

	public void addChildItem(AbstractGanttItem child) {
		if (lstChildItem == null) {
			lstChildItem = new ArrayList<AbstractGanttItem>();
		}
		this.lstChildItem.add(child);
	}

	public void removeChildItem(AbstractGanttItem child) {
		this.lstChildItem.remove(child);
	}

	protected int date2XCoordinate(Date taskBeginDate) {
		// formulas : (diff in hours) * TIMELINE_COLUMN_WIDTH / 24
		if (ProjectConstants.PROJECT_BEGINDATE.compareTo(getBean().getBeginDate()) == 0) {
			return 0;
		}

		long diffInMillis = getBean().getBeginDate().getTime() - ProjectConstants.PROJECT_BEGINDATE.getTime();
		long diffInHours = diffInMillis / TimeConstants.HOUR_IN_MILLIS;

		return (int) (diffInHours * ZoomConstants.TIMELINE_COLUMN_WIDTH / 24);
	}

	protected Date xCoordinate2Date(Date originalDate, int xDragDiff) {
		// formulas : originalDate + ( xDragDiff / PIXEL_IN_MILLIS )
		Date newDate = new Date(originalDate.getTime() + (xDragDiff * TimeConstants.PIXEL_IN_MILLIS));

		return newDate;
	}

	final public void updateItem(Task task) {
		setBean(task);
		setId(getBean().getId());
		setYCoordinate(getBean().getLevel());
		setXCoordinate(date2XCoordinate(getBean().getBeginDate()));
		additionalItemUpdates();
	}

	abstract protected void additionalItemUpdates();

}
